package com.muy.redis.view.window.redis.view.table.bean;

import com.muy.redis.view.window.redis.enums.RedisValueTypeEnum;
import com.muy.redis.view.window.redis.view.table.AColumnDesc;
import com.muy.redis.view.window.redis.view.table.ColumnShowAttr;
import com.muy.redis.view.window.redis.view.table.TreeLineObject;
import org.apache.commons.compress.utils.Lists;

import java.util.List;
import java.util.function.BiConsumer;

/**
 * @Author jiyanghuang
 * @Date 2023/9/17 13:31
 */
public class RedisListRow extends AbstractTableStringRow{

    @AColumnDesc(nameShow = "Row", order = 1, cellEditable = false, columnShowAttr = ColumnShowAttr.INDEX)
    private Integer row;

    @AColumnDesc(nameShow = "Value", order = 10, vcFun = "updateValue")
    private String value;

    private RedisValueTypeEnum typeEnum;

    private int selectIndex;

    private BiConsumer<RedisListRow, RedisListRow> updateConsumer;

    public static List<RedisListRow> genTableData(List<String> strings, RedisValueTypeEnum typeEnum, int pageIndex, BiConsumer<RedisListRow, RedisListRow> updateConsumer) {
        List<RedisListRow> rows = Lists.newArrayList();
        for (int i = 0; i < strings.size(); i++) {
            RedisListRow redisListRow = new RedisListRow();
            redisListRow.setRow(i);
            redisListRow.setSelectIndex(pageIndex + i);
            redisListRow.setValue(strings.get(i));
            redisListRow.setTypeEnum(typeEnum);
            redisListRow.setUpdateConsumer(updateConsumer);
            rows.add(redisListRow);
        }
        return rows;
    }

    public void updateValue(RedisListRow old, RedisListRow update){
        updateConsumer.accept(old, update);
    }

    @Override
    public <T extends TreeLineObject> T copyThis() {
        RedisListRow redisListRow = new RedisListRow();
        redisListRow.setRow(this.getRow());
        redisListRow.setValue(this.getValue());
        redisListRow.setTypeEnum(this.getTypeEnum());
        redisListRow.setSelectIndex(this.getSelectIndex());
        return (T)redisListRow;
    }


    public Integer getRow() {
        return row;
    }

    public void setRow(Integer row) {
        this.row = row;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public RedisValueTypeEnum getTypeEnum() {
        return typeEnum;
    }

    public void setTypeEnum(RedisValueTypeEnum typeEnum) {
        this.typeEnum = typeEnum;
    }

    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    public BiConsumer<RedisListRow, RedisListRow> getUpdateConsumer() {
        return updateConsumer;
    }

    public void setUpdateConsumer(BiConsumer<RedisListRow, RedisListRow> updateConsumer) {
        this.updateConsumer = updateConsumer;
    }
}
